package com.tbsoft.ehr.service.notify;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.dao.notify.NotifyDao;
import com.tbsoft.ehr.login.LoginInfo;
import com.tbsoft.ehr.model.NotifyFile;
import com.tbsoft.ehr.util.ConfigProps;
import com.tbsoft.ehr.util.FileUtils;
import com.tbsoft.ehr.util.ProcessUtils;

/**
 * @desc 系统公告service实现
 * @author vastdream
 * 
 */
public class NotifyServiceImpl implements NotifyService {
	private String NOTIFY_FILE_PATH = (String) ConfigProps.getContextProperty("notify.file.path");
	private Log log = LogFactory.getLog(getClass());
	private NotifyDao notifyDao;

	public void setNotifyDao(NotifyDao notifyDao) {
		this.notifyDao = notifyDao;
	}

	@Override
	public List<NotifyFile> getNotifyFileByType(String type) {
		LoginInfo loginInfo = (LoginInfo) ActionContext.getContext().getSession().get("loginInfo");
		if (loginInfo.isRole((String) ConfigProps.getContextProperty("ehr.role.admin"))) {
			return notifyDao.getFileListbyType(type, "");
		} else {
			return notifyDao.getFileListbyType(type, loginInfo.getUserId());
		}

	}

	@Override
	public NotifyFile getNotifyFileById(String fileId) {
		return notifyDao.getNotifyFileById(fileId);
	}

	@Override
	public List<NotifyFile> getNotifyFileList(String type, String scId) {

		return notifyDao.getNotifyFileList(type, scId);
	}

	@Override
	public void addNotifyFile(String notifyType, String fileName, String scId, String uploadFile) throws Exception {
		NotifyFile notifyFile = new NotifyFile();
		notifyFile.setFileName(fileName);
		if ("".equals(scId)) {
			scId = null;
		}
		notifyFile.setScId(scId);
		notifyFile.setNotifyType(notifyType);
		notifyDao.save(notifyFile);
		log.info("fileId:" + notifyFile.getFileId());
		saveFile( uploadFile, notifyFile);

	}

	private void saveFile( String uploadFile, NotifyFile notifyFile) throws IOException {
		if (notifyFile.getNotifyType().equals("S")) {
			FileUtils.copyFile(new File(uploadFile),
					new File(NOTIFY_FILE_PATH + File.separator + notifyFile.getFileId()));
		} else {
			String cmd = NOTIFY_FILE_PATH + File.separator + "pdf2swf.exe -t " + uploadFile + " -s flashversion=9 -o "
					+ NOTIFY_FILE_PATH + File.separator + notifyFile.getFileId();
			try{
			ProcessUtils.executeCommand(cmd, 10*1000);
			}catch(Exception e){
				log.error("cmd exception:", e);
			}
		}
	}
	@Override
	public void updateNotifyFile(String fileId, String notifyType, String fileName, String scId, String uploadFile)
			throws Exception {
		NotifyFile nf = notifyDao.getNotifyFileById(fileId);
		nf.setNotifyType(notifyType);
		nf.setFileName(fileName);
		nf.setScId(scId);
		notifyDao.saveOrUpdate(nf);
		if(uploadFile != null && !uploadFile.isEmpty()){
			saveFile(uploadFile, nf);
		}
	}
	@Override
	public void deleteNotifyFile(String fileId) throws Exception {
		notifyDao.logicDelete(NotifyFile.class, Integer.valueOf(fileId));
		File file=new File(NOTIFY_FILE_PATH + File.separator + fileId);
		file.delete();
	}
}
